package com.caizongzhen.www.controller;

import com.caizongzhen.www.dto.VerifyParaDto;
import com.caizongzhen.www.entity.*;
import com.caizongzhen.www.service.HistroyService;
import com.caizongzhen.www.service.IllnessKindService;
import com.caizongzhen.www.service.IllnessService;
import com.caizongzhen.www.service.PageViewService;
import com.caizongzhen.www.util.Assert;
import com.caizongzhen.www.util.RespResult;
import com.caizongzhen.www.vo.request.FeedBackRequestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class IllnessController {

    @Autowired
    private IllnessService illnessService;
    @Autowired
    private PageViewService pageViewService;
    @Autowired
    private IllnessKindService illnessKindService;
    @Autowired
    private HistroyService histroyService;

    /*
    * 添加、修改疾病
    * */
    @PostMapping("/illness/save")
    @ResponseBody
    public RespResult illnessSave(Illness illness){
        VerifyParaDto verifyParaDto = new VerifyParaDto();
        verifyParaDto = judgeIllnessSave(illness);
        if (!verifyParaDto.Tag) return RespResult.fail(verifyParaDto.Msg);
        if (Assert.isEmpty(illness.getId())) {
            illness.setCreateTime(new Date());
            illness.setUpdateTime(new Date());
        }
        else illness.setUpdateTime(new Date());
        boolean bool = illnessService.saveOrUpdate(illness);
//        if (Assert.isEmpty(illness.getId()) && bool) {
//            PageView pageView = new PageView();
//            pageView.setPage_views(1);
//            pageView.setIllness_id();
//            // pageView也要对应添加数据
//            boolean bool2 = pageViewService.save();
//        }
        if(bool) return RespResult.success("操作成功");
        else return RespResult.fail("操作失败");
    }

    /*
     *  疾病百科页面
     * */
    @RequestMapping("/findIllness")
    public String findllnessByName(Model model, String illnessName, Integer kind, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer page) {
        List<Illness> illnessList = new ArrayList<>();
        if (!Assert.isEmpty(kind)) {
            illnessList = illnessService.getIllnessDateByIllnessKind(kind);
            model.addAttribute("illness",illnessList);
        }
        else {
            illnessList = illnessService.getIllnessPageDateByIllnessName(illnessName,pageSize,page);
            model.addAttribute("page",page);
            model.addAttribute("size",IllnessService.total);
            model.addAttribute("illness",illnessList);
        }
        // 历史记录
//        List<Histroy> histroys = histroyService.list();
//        model.addAttribute("history",histroys);
        return  "search-illness";
    }


    /*
     *  疾病详情页面
     * */
    @RequestMapping("/findIllnessOne")
    public String findIllnessOne(Model model,Integer id) {
        Illness illness = illnessService.getById(id);
        model.addAttribute("illness",illness);
        return  "illness-reviews";
    }

    /*
     * 疾病管理页面
     * */
    @RequestMapping("/all-illness")
    public String allIllness(Model model, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize, HttpSession session) {
        // 判断是否登录
        User user =(User) session.getAttribute("loginUser");
        if (Assert.isEmpty(user)) return "redirect:/index";
        List<Illness> allIllness = illnessService.getIllnessPageDate(currentPage,pageSize);
        model.addAttribute("illnesses",allIllness);
        return  "all-illness";
    }

    /*
    *  添加、修改页面
    * */
    @RequestMapping("/add-illness")
    public String addMedicla(Model model,Integer id,HttpSession session) {
        Illness illness = new Illness();
        List<IllnessKind> illnessKinds = illnessKindService.list();
        model.addAttribute("kinds",illnessKinds);
        // 判断是否登录
        User user =(User) session.getAttribute("loginUser");
        if (Assert.isEmpty(user)) return "redirect:/index";
        if (!Assert.isEmpty(id)) {
            illness = illnessService.getById(id);
        }
        model.addAttribute("illness",illness);
        return  "add-illness";
    }


    /*
    * 删除疾病信息
    * */
    @PostMapping("/illness/delete")
    @ResponseBody
    public RespResult illnessDelete(Integer id, HttpSession session){
        if (Assert.isEmpty(id)) return RespResult.fail("id不能为空");
        Illness illness = illnessService.getById(id);
        if (Assert.isEmpty(illness)) return RespResult.fail("传入的id有误");
        boolean bool = illnessService.removeById(id);
        if (bool) return RespResult.success("删除成功");
        else return RespResult.fail("删除失败");
    }

    /*
    *  判断新增疾病是否合法
    * */
    private VerifyParaDto judgeIllnessSave(Illness illness) {
        VerifyParaDto verifyParaDto = new VerifyParaDto();
        verifyParaDto.Tag = false;
        if (Assert.isEmpty(illness.getIllnessName())) verifyParaDto.Msg = "疾病名称不能为空";
        else if (Assert.isEmpty(illness.getIncludeReason())) verifyParaDto.Msg = "疾病原因不能为空";
        else if (Assert.isEmpty(illness.getIllnessSymptom())) verifyParaDto.Msg = "主要症状不能为空";
        else if (Assert.isEmpty(illness.getKindId())) verifyParaDto.Msg = "疾病类别不能为空";
        else verifyParaDto.Tag = true;
        return verifyParaDto;
    }
}
